# from main import app
from pydantic import BaseModel
from utils.jwt.gettoken import get_token, verify_token
from src.pc.api.api import redisToken, invalid_token
from typing import Optional, Union
# 小程序配置
from utils.sql.public.config import config, updateConfig
from utils.sql.public.index import indexs, pcindexs, pcindexOne, updatemenu, newmenu, deletemenu
from fastapi import APIRouter, Header

pu_router = APIRouter()
"""
验证token
:param token token
:author scw 2024年3月5日
"""


class Token(BaseModel):
    token: str


@pu_router.post('/api/app/verifyToken/{open_id}')
async def verify_tokens(data: Token):
    return await verify_token(data.token)


# 获取小程序配置
@pu_router.get('/api/manday/pu/config')
async def get_config():
    return config()


# 获取小程序配置PC端
@pu_router.get('/api/manday/pu/pcconfig')
async def get_configs(Authorization: Optional[str] = Header(None)):
    user_data = await redisToken(Authorization)
    if user_data:
        return config()
    else:
        return invalid_token


# 修改小程序配置PC端
class PcConfigs(BaseModel):
    phone: bool
    name: bool
    location: bool


@pu_router.post('/api/manday/pu/updatepcconfig')
async def update_config(data: PcConfigs, Authorization: Optional[str] = Header(None)):
    user_data = await redisToken(Authorization)
    if user_data:
        return updateConfig(data)
    else:
        return invalid_token


# 获取小程序首页功能列表(仅显示)
@pu_router.get('/api/manday/pu/index')
async def get_indexs():
    return indexs()


# 获取小程序首页菜单列表（所有）
@pu_router.get('/api/manday/pu/indexall')
async def get_pc_indexs(query: Union[int, str], pageNum: Union[int, str], pageSize: Union[int, str],
                        Authorization: Optional[str] = Header(None)):
    user_data = await redisToken(Authorization)
    if user_data:
        return pcindexs(query, pageNum, pageSize)
    else:
        return invalid_token


# 获取小程序首页单个菜单
@pu_router.get('/api/manday/pu/menu/{menuid}')
async def get_pc_indexs(menuid: Union[int, str], Authorization: Optional[str] = Header(None)):
    user_data = await redisToken(Authorization)
    if user_data:
        return pcindexOne(menuid)
    else:
        return invalid_token


# 修改小程序首页单个菜单
class PcMenu(BaseModel):
    icon: str
    link: str
    name: str
    isTabBar: int
    isShow: int
    id: int = None


@pu_router.post('/api/manday/pu/menu')
async def get_pc_updatemenu(data: PcMenu, Authorization: Optional[str] = Header(None)):
    user_data = await redisToken(Authorization)
    if user_data:
        return updatemenu(data)
    else:
        return invalid_token


# 新增菜单
@pu_router.post('/api/manday/pu/newmenu')
async def get_pc_newmenu(data: PcMenu, Authorization: Optional[str] = Header(None)):
    user_data = await redisToken(Authorization)
    if user_data:
        return newmenu(data)
    else:
        return invalid_token


# 删除菜单
@pu_router.get('/api/manday/pu/deletemenu/{menuid}')
async def get_pc_deletemenu(menuid: Union[int, str], Authorization: Optional[str] = Header(None)):
    user_data = await redisToken(Authorization)
    if user_data:
        return deletemenu(menuid)
    else:
        return invalid_token
